import { blockUser, collectUser, delBlockUser, delCollectedUser, getUserInfo, getUserOnlineResume } from "@/api/common";
import { Message } from "element-ui";
import Vue from "vue";
import Vuex from "vuex"

Vue.use(Vuex)

const actions = {
    // 切换main页面的加载条状态
    changeLoading(context,val){
        context.commit("CHANGELOADING",val);
    },
    // 通过socket发送聊天(目前不用)
    sendMsg(context,val){
        context.commit("SENDMSG",val);
    }, 
    // 更新websocket(目前不用)
    updateWebSocket(context,socket){
        context.commit("UPDATEWEBSOCKET",socket);
    },
    // 在线简历显示，包括获取信息
    resumeDialogVisible(context,userId){
        console.log(userId);
        
        context.commit("ResumeDialogVisible",userId);
    },
    // 设置vuex个人信息
    setSelfInfo(context,val){
        context.commit("SETSELFINFO",val);
    },
    // 切换用户收藏状态，传递用户对象
    collectStatusChange(context, val) {
        if (val.isCollected) {
            val.isCollected = false;
            //处于收藏状态,调用取消收藏的axios
            delCollectedUser({"userId":val.userId}).then(res=>{
                if (res.code==200) {
                    Message.success("取消收藏成功")
                }
            })
        } else {
            val.isCollected = true;
            //未出于收藏状态，调用收藏的axios
            collectUser({"userId":val.userId}).then(res=>{
                if (res.code==200) {
                    Message.success("收藏成功")
                }
            })
        }
        
    },
    // 切换拉黑状态，传递用户对象
    blockStatusChange(context, val) {
        if (val.isBlocked) {
            val.isBlocked = false;
            //处于拉黑状态,调用取消拉黑的api
            delBlockUser({"userId":val.userId}).then(res=>{
                if (res.code==200) {
                    Message.success("取消拉黑成功")
                }
            })
        } else {
            val.isBlocked = true;
            //未出于拉黑状态，调用拉黑的api
            blockUser({"userId":val.userId}).then(res=>{
                if (res.code==200) {
                    Message.success("拉黑成功")
                }
            })
        }
    }
    
    
}
const mutations = {
    CHANGELOADING(state,val){
        state.vLoading=!state.vLoading
        if (val!=null) {
            state.vLoading=val
            
        }
    },
    SENDMSG(state,val){
        // if (state.webSocket.i) {
            
        // }
        state.webSocket.send(JSON.stringify(val))

    },
    UPDATEWEBSOCKET(state,socket){
        state.webSocket=socket

    },
    ResumeDialogVisible(state,userId){
        if (userId==null) {
            state.resumeDialogVisible=!state.resumeDialogVisible
            return
        }
        getUserOnlineResume({
            "userId":userId
        }).then(res=>{
            if (res.code==200) {
                state.resumeInfo=res.data
                getUserInfo({"userId":userId}).then(res=>{
                state.resumeInfo.userDO=res.data
                state.resumeDialogVisible=!state.resumeDialogVisible
            })
            }
            
        })
    },
    SETSELFINFO(state,val){
        state.hrId=val.hrId;
        state.companyId=val.companyId
        state.companyAddress=val.companyAddress
        state.identity=val.identity
        state.headIcon=val.headIcon
        state.hrName=val.hrName
    }
}
const state = {
    test: "测试",
    resumeDialogVisible:false,
    resumeId:null,
    hrId:null,
    hrName:null,
    companyId:null,
    companyAddress:null,
    identity:null,
    headIcon:null,
    resumeInfo:null,
    webSocket:null,
    vLoading:false,
}

const store = new Vuex.Store({
    actions, mutations, state
})

export default store;